package com.ssm.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ssm.bean.MaterialInfo;
import com.ssm.service.MaterialInfoService;

/**
 * @author ZhaoXianming
 * @Description 物料基础信息管理
 * @date 2020年3月12日
 */
@Controller
@RequestMapping(value = "/materialBaseInfo")
public class MaterialBaseInfoController extends BaseController {

	Logger log = LoggerFactory.getLogger(MaterialBaseInfoController.class);
	
	@Autowired
	public MaterialInfoService materialInfoService;
	
	@RequestMapping(value = "/toMaterialBaseInfoJsp")
	public String toMaterialBaseInfo(Model model){
		model.addAttribute("material", new MaterialInfo());
		return "basicInfoManagement/materialInformation";
	}
	
	/**
	 * @Description 查询所有物料信息
	 * @param materialInfo
	 * @param request
	 * @return Map<String,Object>
	 */
	@RequestMapping("/showallmaterialbaseinfo")
	@ResponseBody
	public Map<String,Object> queryMaterialsByParams(@RequestBody MaterialInfo materialInfo, 
			HttpServletRequest request){
		Map<String,Object> params = getPageParams(request);
		params.put("materialInfo", materialInfo);
		List<MaterialInfo> list = materialInfoService.selectAllMaterialInfoWithDev(params);
		params.put("materialList", list);
		return params;
	}
	
	/**
	 * @Description 判断物料是否重复
	 * @param materialCode
	 * @return boolean
	 */
	@RequestMapping("/isMaterialRepeat")
	@ResponseBody
	public boolean isMaterialRepeat(@RequestParam("materialCode") String materialCode, HttpServletRequest request){
		boolean flag = materialInfoService.isMaterialRepeat(materialCode);
		return !flag;
	}
	
	/**
	 * @Description 判断RFID卡号是否已绑定
	 * @param rfidCardNumber
	 * @return boolean
	 */
	@RequestMapping("/isRfidCardNumberRepeat")
	@ResponseBody
	public boolean isRfidCardNumberRepeat(@RequestParam("rfidCardNumber") String rfidCardNumber, HttpServletRequest request){
		boolean flag = materialInfoService.isRfidCardNumberRepeat(rfidCardNumber);
		return !flag;
	}
	
	/**
	 * @Description 新增物料信息
	 * @param materialInfo
	 * @return String
	 */
	@RequestMapping("/materialAddOrUpdate")
	@ResponseBody
	public String insertOrUpdateMaterialInfo(@RequestBody MaterialInfo materialInfo){
		try{
			materialInfoService.insertOrUpdateMaterialInfo(materialInfo);
			return "success";
		}catch(Exception e){
			log.error("新增物料失败！", e);
			return "error";
		}
	}
	
	/**
	 * @Description 跳转到新增物料信息页面
	 * @return String
	 */
	@RequestMapping("/toMaterialAddJsp")
	public String toMaterialInfoAddJsp(){
		return "basicInfoManagement/materialAdd";
	}
	
	/**
	 * @Description 跳转到物料信息修改页面
	 * @param model
	 * @param request
	 * @return String
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/toMaterialUpdateJsp")
	public String toMaterialInfoUpdateJsp(Model model, HttpServletRequest request) throws UnsupportedEncodingException{
		String materialCode = new String(request.getParameter("materialCode").getBytes("iso-8859-1"), "utf-8");
		MaterialInfo info = materialInfoService.selectMaterialByCode(materialCode);
		model.addAttribute("material", info);
		return "basicInfoManagement/materialAdd";
	}
	
	/**
	 * @Description 解绑RFID卡
	 * @param materialCode
	 * @return String
	 */
	@RequestMapping("/deleteRfidCard")
	@ResponseBody
	public String deleteRfidCard(String materialCode){
		try{
			String materialCode1 = new String(materialCode.getBytes("iso-8859-1"), "utf-8");
			materialInfoService.deleteRfidCard(materialCode1);
			return "success";
		}catch(Exception e){
			return "error";
		}
	}
	
	/**
	 * @Description 绑定RFID卡
	 * @param materialCode
	 * @param rfidCardNumber
	 * @return String
	 */
	@RequestMapping("/bindRfidCard")
	@ResponseBody
	public String bindRfidCard(String materialCode,String rfidCardNumber){
		try{
			String materialCode1 = new String(materialCode.getBytes("iso-8859-1"), "utf-8");
			materialInfoService.bindRfidCard(materialCode1, rfidCardNumber);
			return "success";
		}catch(Exception e){
			return "error";
		}
	}
	
	/**
     * 删除物料信息
     */
    @RequestMapping(value = "/deletematerialbaseinfo", method = RequestMethod.POST)
    @ResponseBody
    public String deleteMaterialBaseInfo(String[] ids)
    {
    	try{
    		List<String> list = Arrays.asList(ids);
    		return materialInfoService.deleteMaterialBaseInfo(list);
    	}catch(Exception e){
    		return "error";
    	}
    }
    
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    public String uploadExcel(HttpServletRequest request, 
    		@RequestParam("file") MultipartFile file) throws Exception
    {
    	materialInfoService.saveFileToMysql(file);
        return "basicInfoManagement/materialInformation";
    }
}
